//
//  RBAddressAddVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/15.
//

#import "RBAddressAddVC.h"
#import <UIColor+YYAdd.h>
#import "RBThemeConfig.h"
#import <UIControl+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBDefine.h"
#import <Masonry.h>
#import "UIButton+RBAdd.h"
#import "RBProfileVC.h"
#import "UIScrollView+RBAdd.h"
#import "RBTextField.h"
#import "RBZoneChooseVC.h"
#import "UIViewController+RBAdd.h"

@interface RBAddressAddVC ()

@property(nonatomic, strong)RBTextField *nameF;
@property(nonatomic, strong)RBTextField *phoneF;
@property(nonatomic, copy) NSString *province;
@property(nonatomic, copy) NSString *city;
@property(nonatomic, copy) NSString *zone;
@property(nonatomic, strong)RBTextField *areaF;
@property(nonatomic, strong)RBTextField *addressF;
@property(nonatomic, strong) UISwitch *defaultSwitch;
@property(nonatomic, assign) BOOL isDefault;
@property(nonatomic, strong) UIButton *confirmBtn;

@end

@implementation RBAddressAddVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"新增地址";//修改地址
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    
    [self.view view_addView:({
        UIView *view = [self cellWithTitle:@"收货人姓名" palceHolder:@"填写收货人姓名" needArrow:NO];
        self.nameF = (RBTextField *)[view viewWithTag:3];
        view;
    }) withHeight:Zoom(55) width:kRBMainScreenWidth top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:({
        UIView *view = [self cellWithTitle:@"联系电话" palceHolder:@"填写联系电话" needArrow:NO];
        self.phoneF = (RBTextField *)[view viewWithTag:3];
        self.phoneF.type = RBTextFieldTypePhone;
        view;
    }) withHeight:Zoom(55) width:kRBMainScreenWidth top:Zoom(0) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:({
        UIView *view = [self cellWithTitle:@"选择地区" palceHolder:@"请选择省 / 市 / 区" needArrow:YES];
        [view rbAddTapGesWithTarget:self action:@selector(areaBtnTapped)];
        self.areaF = (RBTextField *)[view viewWithTag:3];
        self.areaF.userInteractionEnabled = NO;
        view;
    }) withHeight:Zoom(55) width:kRBMainScreenWidth top:Zoom(0) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:({
        UIView *view = [self cellWithTitle:@"详细地址" palceHolder:@"请填写具体门牌信息" needArrow:NO];
        self.addressF = (RBTextField *)[view viewWithTag:3];
        view;
    }) withHeight:Zoom(55) width:kRBMainScreenWidth top:Zoom(0) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.view view_addView:({
        UIView *view = [self cellWithTitle:@"默认地址" palceHolder:nil needArrow:NO];
        [view viewWithTag:3].userInteractionEnabled = NO;
        self.defaultSwitch = UISwitch.new;
        self.defaultSwitch.onTintColor = [UIColor colorWithHexString:@"#518CFF"];
        [self.defaultSwitch addTarget:self action:@selector(defaultSwitchValueChanged) forControlEvents:UIControlEventValueChanged];
        [view addSubview:self.defaultSwitch];
        [self.defaultSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-Zoom(18));
        }];
        view;
    }) withHeight:Zoom(55) width:kRBMainScreenWidth top:Zoom(0) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    
    self.confirmBtn.hidden = NO;

    if(self.model)
    {
        self.nameF.text = self.model.name;
        self.phoneF.text = self.model.mobile;
        self.province = self.model.province;
        self.city = self.model.city;
        self.zone = self.model.area;
        self.areaF.text = [NSString stringWithFormat:@"%@ %@ %@", self.province, self.city, self.zone];
        self.addressF.text = self.model.address;
        self.isDefault = self.model.moren.intValue == 1;
    }
}

#pragma mark - Network

#pragma mark - Event

- (void)areaBtnTapped
{
    __weak typeof(self) weak_self = self;
    RBZoneChooseVC *vc = RBZoneChooseVC.new;
    vc.block = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull zone) {
        weak_self.province = province;
        weak_self.city = city;
        weak_self.zone = zone;
        weak_self.areaF.text = [NSString stringWithFormat:@"%@ %@ %@", province, city, zone];
    };
    [self presentVc:vc];
}

- (void)defaultSwitchValueChanged
{
    self.isDefault = self.defaultSwitch.on;
}

- (void)confirmBtnTapped
{
    __weak typeof(self) weak_self = self;
    if(!self.nameF.text.length)
    {
        [self.view showTip:self.nameF.placeholder completion:nil];
        return;
    }
    if(!self.phoneF.validate)
    {
        [self.view showTip:@"请输入正确手机号" completion:nil];
        return;
    }
    if(!self.areaF.text.length)
    {
        [self.view showTip:self.areaF.placeholder completion:nil];
        return;
    }
    if(!self.addressF.text.length)
    {
        [self.view showTip:self.addressF.placeholder completion:nil];
        return;
    }
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"name"] = self.nameF.text;
        dicM[@"mobile"] = self.phoneF.text;
        dicM[@"province"] = self.province;
        dicM[@"city"] = self.city;
        dicM[@"area"] = self.zone;
        dicM[@"address"] = self.addressF.text;
        dicM[@"moren"] = self.isDefault? @"1": @"0";
        dicM[@"add_id"] = self.model.add_id;
    } path:self.model? @"hu_address_update": @"hu_address_add" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
        [weak_self.navigationController popViewControllerAnimated:YES];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Setter

- (void)setIsDefault:(BOOL)isDefault
{
    if(_isDefault == isDefault) return;
    _isDefault = isDefault;
    self.defaultSwitch.on = isDefault;
}

#pragma mark - Getter

- (UIView *)cellWithTitle: (NSString *)title
              palceHolder: (NSString *)placeHoler
                needArrow: (BOOL)needArrow
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleL = ({
        UILabel *view = UILabel.new;
        view.text = title;
        view.textColor = [RBThemeConfig textBlackColor];
        view.font = [UIFont systemFontOfSize:Zoom(14)];
        view;
    });
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(18));
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(78));
    }];
    
    UIImageView *arrowImgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"mine_arrow_right"]];
    arrowImgView.hidden = !needArrow;
    [view addSubview:arrowImgView];
    [arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(18));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(7));
        make.height.mas_equalTo(Zoom(13));
    }];
    
    RBTextField *textF = RBTextField.new;
    textF.placeholder = placeHoler;
    textF.tag = 3;
    textF.textColor = [RBThemeConfig textBlackColor];
    textF.font = [UIFont systemFontOfSize:Zoom(14)];
    [view addSubview:textF];
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleL.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(arrowImgView.mas_left).offset(Zoom(-15));
    }];
    
    return view;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn)
    {
        self.confirmBtn = ({
            UIButton *view = UIButton.new;
            [view rbAddTarget:self action:@selector(confirmBtnTapped)];
            [view rbSetTitle:@"确定"];
            [view rbSetTitleColor:UIColor.whiteColor];
            [view rbSetFont:[UIFont systemFontOfSize:Zoom(15)]];
            [view rbSetCornerRadius:Zoom(22)];
            view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(-MAX(Zoom(25), kRBSafeBottomHeight));
                make.centerX.mas_equalTo(0);
                make.size.mas_equalTo(CGSizeMake(Zoom(339), Zoom(44)));
            }];
            view;
        });
    }
    return _confirmBtn;
}

@end
